﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
#endregion // Using Statements

namespace ParticleSystem
{
    /// <summary>
    /// An object to define a range of values (via a minimum and maximum).
    /// </summary>
    /// <typeparam name="T">The type of values in the range.</typeparam>
    public class Range<T>
    {
        #region Fields
        /// <summary>
        /// The minimum value in this range.
        /// </summary>
        private T mMin;
        public T min
        {
            get { return mMin; }
        }

        /// <summary>
        /// The maximum value in this range.
        /// </summary>
        private T mMax;
        public T max
        {
            get { return mMax; }
        }

        #endregion // Fields

        #region Constructors
        /// <summary>
        /// Range constructor.
        /// </summary>
        /// <param name="min">Minimum value in the range.</param>
        /// <param name="max">Maximum value in the range.</param>
        public Range(T min, T max)
        {
            mMin = min;
            mMax = max;
        }

        /// <summary>
        /// Create a range from an XML element where the min and max are still in XML element representation.
        /// </summary>
        /// <param name="rangeElement">The XML element containing this ramge's information.</param>
        /// <returns>The parsed XmlElement range.</returns>
        public static Range<XmlElement> XmlElementRange(XmlElement rangeElement)
        {
            XmlElement min = null, max = null;

            foreach (XmlElement rangeChildElement in rangeElement.ChildNodes)
            {
                // found range minimum
                if (rangeChildElement.Name.Equals("min"))
                {
                    min = rangeChildElement;
                }
                // found range maximum
                else if (rangeChildElement.Name.Equals("max"))
                {
                    max = rangeChildElement;
                }
            }

            if (min == null || max == null)
            {
                throw new Exception("Could not parse xml element into a range!");
            }

            return new Range<XmlElement>(min, max);
        }
        #endregion // Constructors
    }
}
